package com.chicken.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chicken.pojo.Member;
import com.chicken.service.impl.MemberServiceImpl;
import com.google.gson.Gson;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "/member")
@Controller
@RequestMapping("/member")
public class MemberController {

	@Resource(name = "memberService")
	MemberServiceImpl memberService;

	/**
	 * 获取参数
	 * 
	 * @param Request
	 * @param response
	 * @return
	 */
	public Member getParam(HttpServletRequest Request,
			HttpServletResponse response) {
		Integer id = null;
		Date create_date = null;
		Date modify_date = null;
		String username = null;
		String password = null;
		String area = null;
		String address = null;
		String name = null;
		String login_ip = null;
		Integer ch_farmer_id = null;
		Integer gender = null;
		Date birthday = null;
		Integer sn = null;
		Date buyhencoopTime = null;
		Date hencoopOutTime = null;
		String head_img_url = null;
		String mobile = null;
		if (Request.getParameter("id") != null) {
			id = Integer.valueOf(Request.getParameter("id"));
		}
		if (Request.getParameter("username") != null) {
			username = Request.getParameter("username");
		}
		if (Request.getParameter("password") != null) {
			password = Request.getParameter("password");
		}
		if (Request.getParameter("area") != null) {
			area = Request.getParameter("area");
		}
		if (Request.getParameter("address") != null) {
			address = Request.getParameter("address");
		}
		if (Request.getParameter("name") != null) {
			name = Request.getParameter("name");
		}
		if (Request.getParameter("login_ip") != null) {
			login_ip = Request.getParameter("login_ip");
		}
		if (Request.getParameter("ch_farmer_id") != null) {
			ch_farmer_id = Integer
					.valueOf(Request.getParameter("ch_farmer_id"));
		}
		if (Request.getParameter("gender") != null) {
			gender = Integer.valueOf(Request.getParameter("gender"));
		}
		if (Request.getParameter("sn") != null) {
			sn = Integer.valueOf(Request.getParameter("sn"));
		}
		if (Request.getParameter("head_img_url") != null) {
			head_img_url = Request.getParameter("head_img_url");
		}
		if (Request.getParameter("mobile") != null) {
			mobile = Request.getParameter("mobile");
		}
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			if (Request.getParameter("create_date") != null) {
				create_date = s.parse(Request.getParameter("create_date"));
			}
			if (Request.getParameter("modify_date") != null) {
				modify_date = s.parse(Request.getParameter("modify_date"));
			}
			if (Request.getParameter("buyhencoopTime") != null) {
				modify_date = s.parse(Request.getParameter("buyhencoopTime"));
			}
			if (Request.getParameter("hencoopOutTime") != null) {
				modify_date = s.parse(Request.getParameter("hencoopOutTime"));
			}
			if (Request.getParameter("birthday") != null) {
				birthday = s.parse(Request.getParameter("birthday"));
			}
			if (Request.getParameter("create_date") != null) {
				create_date = s.parse(Request.getParameter("create_date"));
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		Member member = new Member(id, create_date, modify_date, username,
				password, area, address, name, login_ip, ch_farmer_id, gender,
				birthday, sn, buyhencoopTime, hencoopOutTime, head_img_url,
				mobile);

		System.out.println(member.toString());
		return member;
	}

	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping( value = "addUser", method = RequestMethod.POST, produces
	 * = "application/json; charset=utf-8")
	 * 
	 * @ApiOperation(value = "添加用户", httpMethod = "POST", response =
	 * BaseResultVo.class, notes = "add user") public String
	 * addUser(@ApiParam(required = true, name = "postData", value =
	 * "用户信息json数据") @RequestParam( value = "postData") String postData,
	 * HttpServletRequest request) {}
	 */
	@ResponseBody
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	@ApiOperation(value = "查看会员", httpMethod = "GET", response = Member.class, notes = "Show Member")
	public String showMember(HttpServletRequest request,
			HttpServletResponse response) {
		List<Member> memberAll = memberService.memberAll();
		request.setAttribute("memberList", memberAll);
		Gson g = new Gson();
		String json = g.toJson(memberAll);
		return json;
	}

	/**
	 * 添加会员
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addMember", method = RequestMethod.GET)
	@ApiOperation(value = "添加会员", httpMethod = "GET", response = Member.class, notes = "Add Member", produces = "application/json; charset=utf-8")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "name", value = "真实姓名", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "binding", value = "鸡舍绑定", required = true, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "buyhencoopTime", value = "鸡舍购买时间", required = true, dataType = "Date", paramType = "query"),
			@ApiImplicitParam(name = "hencoopOutTime", value = "鸡舍到期时间", required = true, dataType = "Date", paramType = "query"), })
	public String addMember(HttpServletRequest request,
			HttpServletResponse response) {
		Gson gson = new Gson();
		Member param = getParam(request, response);
		int result = memberService.addMember(param);
		if (result > 0) {
			String show = showMember(request, response);
			String json = gson.toJson(show);
			return json;
		} else {
			String json = gson.toJson(result);
			return json;
		}
	}

	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ApiOperation(value = "删除会员", httpMethod = "GET", response = Member.class, notes = "deleteMember", produces = "application/json;charset=utf8")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "会员编号", required = true, dataType = "int", paramType = "query"), })
	public String deleteMember(HttpServletRequest request,
			HttpServletResponse response) {
		Gson gson = new Gson();
		String json = null;
		Integer id = Integer.valueOf(request.getParameter("id"));

		int result = memberService.deleteMember(id);
		if (result > 0) {
			String show = showMember(request, response);
			json = gson.toJson(show);
			return json;
		} else {
			json = gson.toJson(result);
			return json;
		}
	}

	/*
	 * public String updateMember(HttpServletRequest request,
	 * HttpServletResponse response) { Gson gson = new Gson(); String json =
	 * null; Member member = getParam(request, response);
	 * 
	 * int result = memberService.updateMember(member); if(result>0){ String
	 * show = showMember(request, response); json = gson.toJson(show); return
	 * json; }else { json = gson.toJson(result); return json; } }
	 */
}
